package com.iocup.keybastion.spring.starter.config;


import com.ejlchina.okhttps.HTTP;
import com.iocup.keybastion.configuration.SecurityProperties;
import com.iocup.keybastion.core.SecurityEngine;
import com.iocup.keybastion.sso.ClientSsoAuthLogic;
import com.iocup.keybastion.sso.config.SsoApiProperties;
import com.iocup.keybastion.sso.config.SsoProperties;
import com.iocup.keybastion.sso.core.ClientSsoAuthLogicImpl;
import com.iocup.keybastion.sso.core.DefaultSsoAuthFunction;
import com.iocup.keybastion.sso.core.DefaultSsoHttpActionAdapter;
import com.iocup.keybastion.sso.core.SsoAuthFunction;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration;

/**
 * @author xyjxust
 * @create 2022/3/23 17:34
 **/
@Configuration
@Conditional({EnabledSsoServerCondition.class})
public class SsoServerConfiguration {


    @Bean
    @ConditionalOnMissingBean(SsoAuthFunction.class)
    public SsoAuthFunction ssoAuthFunction(SsoProperties ssoProperties, SecurityEngine securityEngine) {
        return new DefaultSsoAuthFunction(ssoProperties, securityEngine.getAuthenticationConfigurator().getCacheStore());
    }
    @Bean
    public DefaultSsoHttpActionAdapter ssoHttpActionAdapter(SsoAuthFunction ssoAuthFunction, SecurityProperties securityProperties){
        DefaultSsoHttpActionAdapter httpActionAdapter = new DefaultSsoHttpActionAdapter();
        httpActionAdapter.setSsoAuthFunction(ssoAuthFunction);
        httpActionAdapter.setSecurityProperties(securityProperties);
        return httpActionAdapter;
    }
    @Bean
    public ClientSsoAuthLogic clientSsoAuthLogic(SsoProperties ssoProperties,
                                                 SsoApiProperties ssoApiProperties,
                                                 SsoAuthFunction ssoAuthFunction,
                                                 SecurityEngine securityEngine, HTTP http){
        ClientSsoAuthLogic clientSsoAuthLogic  =   new ClientSsoAuthLogicImpl(ssoProperties, ssoApiProperties, ssoAuthFunction,securityEngine.getAuthenticationConfigurator().getCacheStore(),
                securityEngine.getAuthenticationConfigurator().getUserDetailService());
        clientSsoAuthLogic.setHttp(http);
        return clientSsoAuthLogic;
    }

}
